package com.lenovo.smarttraffic.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.RangeMonthView;
import com.lenovo.smarttraffic.util.VolleyUtil;

/**
 * Created by Caleb.F
 * on 2020-03-09
 * at 20:21
 */
public class MyRangeView extends RangeMonthView {
    private int mRadius;

    public MyRangeView(Context context) {
        super(context);
    }

    /**
     * 绘制前的准备工作
     */
    @Override
    protected void onPreviewHook() {
        super.onPreviewHook();
        //绘制的圆的半径
        mRadius = Math.min(mItemHeight, mItemWidth) *2/5;
    }

    //绘制选中的背景
    /**
     * 绘制选中的日期
     *
     * @param canvas         canvas
     * @param calendar       日历日历calendar
     * @param x              日历Card x起点坐标
     * @param y              日历Card y起点坐标,每个小框的左上角
     * @param hasScheme      hasScheme 非标记的日期
     * @param isSelectedPre  上一个日期是否选中
     * @param isSelectedNext 下一个日期是否选中
     * @return 是否继续绘制onDrawScheme，true or false
     */
    @Override
    protected boolean onDrawSelected(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelectedPre, boolean isSelectedNext) {
        int cx = x + mItemWidth / 2;
        int cy = y + mItemHeight / 2;
        if (isSelectedPre) {
            if (isSelectedNext) {
              //周日
                if (calendar.getWeek() == 0) {
                    drawFirstItem(canvas,x,y);
                }
                //周六
                else if (calendar.getWeek() == 6) {
                    drawLastItem(canvas,x,y);
                }
                else {
                    //cy-radius+2redius
                    canvas.drawRect(x, cy - mRadius, x + mItemWidth, cy + mRadius, mSelectedPaint);
               }

            } else {
                //末尾
                drawLastItem(canvas, x, y);
            }
        } else {
            if (isSelectedNext) {
                //开头
                drawFirstItem(canvas, x, y);
            } else {
                //单选
                canvas.drawCircle(cx, cy, mRadius, mSelectedPaint);
            }
        }
        //不对特殊日期处理
        return false;
    }



    @Override
    protected void onDrawScheme(Canvas canvas, Calendar calendar, int x, int y, boolean isSelected) {
//没必要写
    }


    /**
     *     绘制日历文本
     */
    @Override
    protected void onDrawText(Canvas canvas, Calendar calendar, int x, int y, boolean hasScheme, boolean isSelected) {
        float myBaseLine = mTextBaseLine + y;
        canvas.drawText(String.valueOf(calendar.getDay()), x + mItemWidth / 2, myBaseLine, mSelectTextPaint);
    }

    private void drawFirstItem(Canvas canvas, int x, int y) {
        int cx=x+mItemWidth/2;
        int cy=y+mItemHeight/2;
        canvas.drawRoundRect(new RectF(x, cy - mRadius, x + mItemWidth, cy + mRadius), 15, 15, mSelectedPaint);
        canvas.drawRect(cx, cy - mRadius, x + mItemWidth, cy + mRadius, mSelectedPaint);
    }

    private void drawLastItem(Canvas canvas, int x, int y) {
        int cx=x+mItemWidth/2;
        int cy=y+mItemHeight/2;
        canvas.drawRect(x,cy - mRadius , cx, cy + mRadius, mSelectedPaint);
        canvas.drawRoundRect(new RectF(x, cy - mRadius, x + mItemWidth, cy + mRadius), 15, 15, mSelectedPaint);
    }

}
